package com.cloud.bssp.ThreadPool;

import cn.hutool.core.thread.NamedThreadFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @description： 线程池测试
 * @author：weirx
 * @date：2021/8/6 17:26
 * @version：3.0
 */
public class ThreadPoolExecutorTest {

    public static void setParam(ThreadPoolExecutor executor) {
        executor.setCorePoolSize(20);
        executor.setMaximumPoolSize(40);
        executor.setThreadFactory(new NamedThreadFactory("这里是自定义的线程名称", false));
    }

    public static void getParam(ThreadPoolExecutor executor) {
        System.out.println("当前核心线程数：" + executor.getCorePoolSize());
        System.out.println("当前最大线程数：" + executor.getMaximumPoolSize());
        System.out.println("当前活跃线程数：" + executor.getActiveCount());
        System.out.println("当前处理任务数：" + executor.getTaskCount());
        System.out.println("当前排队数：" + executor.getQueue().size());
    }

    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                10,
                20,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue(10),
                new cn.hutool.core.thread.NamedThreadFactory("weirx", false));
        ThreadPoolExecutorTest.getParam(executor);
        for (int i = 0; i < 100; i++) {
            executor.execute(() -> {
                System.out.println(Thread.currentThread().getName());
            });
            if (i == 10) {
                ThreadPoolExecutorTest.setParam(executor);
            }
            ThreadPoolExecutorTest.getParam(executor);
        }
    }
}
